#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('form-xobject');

my $n_tests = 22;

$td->runtest("form xobject creation",
             {$td->COMMAND => "test_driver 55 fxo-red.pdf"},
             {$td->STRING => "test 55 done\n", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("compare files",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "form-xobjects-out.pdf"});
foreach (my $i = 56; $i <= 59; ++$i)
{
    # See comments in test_driver.cc for a verbal description of what
    # the resulting files should look like.
    $td->runtest("overlay transformations",
                 {$td->COMMAND => "test_driver $i fxo-red.pdf fxo-blue.pdf"},
                 {$td->STRING => "test $i done\n", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    $td->runtest("compare files",
                 {$td->FILE => "a.pdf"},
                 {$td->FILE => "fx-overlay-$i.pdf"});
}
foreach (my $i = 64; $i <= 67; ++$i)
{
    # See comments in test_driver.cc for a verbal description of what
    # the resulting files should look like.
    $td->runtest("overlay shrink/expand",
                 {$td->COMMAND =>
                      "test_driver $i fxo-bigsmall.pdf fxo-smallbig.pdf"},
                 {$td->STRING => "test $i done\n", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    $td->runtest("compare files",
                 {$td->FILE => "a.pdf"},
                 {$td->FILE => "fx-overlay-$i.pdf"});
}

my @uo_cases = (
    '--underlay fxo-green.pdf --repeat=z --to=1-14 --' .
    ' --overlay --file=fxo-blue.pdf --', # 1
    '--overlay fxo-green.pdf --from= --repeat=r2,r1 --' .
    ' --underlay --file=fxo-blue.pdf --from=z-1 --', # 2
    '--overlay fxo-green.pdf --from= --repeat=r2,r1 --' .
    ' --underlay fxo-blue.pdf --from=z-1 -- --coalesce-contents', # 3
    '--overlay fxo-green.pdf --', # 4
    '--underlay fxo-green.pdf --to=3-7 --', # 5
    '--overlay fxo-blue.pdf --to=1,1,1,1 --from=1-4 --' .
    ' --pages . 1 --', #6
    '--overlay 20-pages.pdf --password=user --', #7
    '--overlay fxo-blue.pdf --to=1-4 --from=1-4 --' .
    ' --overlay fxo-green.pdf --to=1-4 --from=4-1 --' .
    ' --underlay fxo-blue.pdf --to=4-1 --from=5-8 --' .
    ' --underlay fxo-green.pdf --to=4-1 --from=8-5 --' .
    ' --pages . 1-4 --', #8
    );
$n_tests += 2 * scalar(@uo_cases);
for (my $i = 1; $i <= scalar(@uo_cases); ++$i)
{
    my $args = $uo_cases[$i-1];
    my $outbase = "uo-$i";
    $td->runtest("overlay/underlay $i",
                 {$td->COMMAND =>
                      "qpdf --static-id --qdf --no-original-object-ids" .
                      " --verbose fxo-red.pdf a.pdf $args"},
                 {$td->FILE => "$outbase.out", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    $td->runtest("compare files",
                 {$td->FILE => "a.pdf"},
                 {$td->FILE => "$outbase.pdf"});
}
$td->runtest("foreach",
             {$td->COMMAND => "test_driver 71 nested-form-xobjects.pdf"},
             {$td->FILE => "nested-form-xobjects.out",
                  $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("page operations on form xobject",
             {$td->COMMAND => "test_driver 72 nested-form-xobjects.pdf"},
             {$td->FILE => "page-ops-on-form-xobject.out",
                  $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("overlay on page with no resources",
             {$td->COMMAND =>
                  "qpdf --deterministic-id page-with-no-resources.pdf" .
                  " --overlay minimal.pdf -- a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check overlay with no resources output",
             {$td->COMMAND => "qpdf-test-compare a.pdf overlay-no-resources.pdf"},
             {$td->FILE => "overlay-no-resources.pdf", $td->EXIT_STATUS => 0});

cleanup();
$td->report($n_tests);
